#include "zhongzhitablewidget.h"
#include "QStringList"
#include "QComboBox"
#include "utils/zhongzhimanager.h"
#include "setting.h"

ZhongZhiTableWidget::ZhongZhiTableWidget(QWidget *parent) : QTableWidget(parent)
{

}

void ZhongZhiTableWidget::init(){
    QStringList list;
    list<< "角色序号" << "农作物" << "早产晚产" << "操作";
    QStringList tip;
    tip<< "与角色管理中的序号对应,表示角色管理中的此角色进行此项跑商设置"<< "农作物" << "早产晚产" << "操作";

    setHorizontalHeaderLabels(list);
    setFocusPolicy(Qt::FocusPolicy::NoFocus);
    QHeaderView *headerView = horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch); //自适应列宽
    headerView->setMinimumHeight(40);
    headerView->setStyleSheet("QHeaderView::section {background-color: #ededed; border-bottom:1px solid #000000}");
    headerView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
    headerView->setHighlightSections(false);

    for(int i = 0 ; i < list.size(); i++){
        QTableWidgetItem *item = new QTableWidgetItem(list.at(i));
        QFont font = this->font();
        font.setBold(true);
        item->setFont(font);
        item->setFlags(Qt::ItemFlag::NoItemFlags);
        item->setSelected(false);
        item->setBackground(Qt::gray);
        if(i != list.size() -1){
            item->setForeground(Qt::red);
        }
        item->setToolTip(tip.at(i));
        setHorizontalHeaderItem(i, item);
    }

    QList<ZhongZhiBean> zhongzhiBeans;
    readZhongZhiBeansSetting(zhongzhiBeans);
    if(!zhongzhiBeans.isEmpty()){
        for(ZhongZhiBean runshopBean : zhongzhiBeans){
            creatNewRow(runshopBean.num, runshopBean.crop,runshopBean.type);
        }
    }
}

int ZhongZhiTableWidget::creatNewRow(int num, QString crop, QString type)
{
    if(!isCreateTableFinish())return -1;
    int row = rowCount();
    setRowCount(row  + 1);

    //序号
    QTableWidgetItem *item111 = new QTableWidgetItem();
    item111->setTextAlignment(Qt::AlignCenter);
    item111->setSelected(false);
    item111->setText(num == 0 ? "" :QString::number(num));
    this->setItem(row ,numType, item111);

    //农作物
    QComboBox *cropsBox = new QComboBox(this);
    QStringList crops = ZhongZhiManager::getInstance()->getCrops();
    cropsBox->addItems(crops);

    cropsBox->setCurrentIndex(crops.indexOf(crop));
    cropsBox->setEditable(false);
    setCellWidget(row ,cropType, cropsBox);

    //作物类型 早产 晚产
    QComboBox *cropsTypeBox = new QComboBox(this);
    QStringList cropsType = ZhongZhiManager::getInstance()->getCropType();
    cropsTypeBox->addItems(cropsType);
    cropsTypeBox->setCurrentIndex(cropsType.indexOf(type));
    cropsTypeBox->setMaxCount(2);
    cropsTypeBox->setEditable(false);
    setCellWidget(row ,productType, cropsTypeBox);

    //操作 删除
    QPushButton *deleteButton = new QPushButton(this);
    deleteButton->setText("删除");
    deleteButton->setStyleSheet("color:red");
    setCellWidget(row, deleteType, deleteButton);
    connect(deleteButton, &QPushButton::clicked,this,[&](){
        this->removeRow(currentRow());
    }); // 再连接信号

    return 0;
}

bool ZhongZhiTableWidget::isCreateTableFinish(){
    int row = rowCount();
    if(row == 0) return true;
    bool isFinish = true;
    for(int i = 0 ; i< columnCount() - 1; i++){
        if(i == cropType || i == productType || i == deleteType){
            continue;
        }
        QString content = item(row - 1,i)->text();
        isFinish = isFinish && !content.isEmpty();
    }
    return isFinish;
}


bool ZhongZhiTableWidget::getAllTableBeans(QList<ZhongZhiBean*> *allZhongZhiBeans)
{
    if(allZhongZhiBeans == nullptr){
        return false;
    }
    qDeleteAll(allZhongZhiBeans->begin(), allZhongZhiBeans->end());
    allZhongZhiBeans->clear();
    for(int i = 0 ; i < rowCount() ; i++){
        ZhongZhiBean *runShopBean = new ZhongZhiBean();
        if(getBean(i, runShopBean)){
            allZhongZhiBeans->append(runShopBean);
        }
    }
    if(allZhongZhiBeans->isEmpty()){
        return false;
    }
    return true;
}

bool ZhongZhiTableWidget::getBean(int row, ZhongZhiBean *zhongZhiBean){
    bool isOk = false;
    QString numStr = item(row ,numType)->text();
    int num = numStr.toInt(&isOk);
    if(!isOk){
        emit logEvent("角色序号设置错误");
        return false;
    }
    zhongZhiBean->num = num;

    QComboBox *comboBox = (QComboBox*)cellWidget(row,cropType);
    QString crop = comboBox->currentText();
    if(!crop.isEmpty()){
        zhongZhiBean->crop = crop;
    }else{
         emit logEvent("请选择农作物");
        return false;
    }

    //早产 晚产
    QComboBox *timeBox = (QComboBox*)cellWidget(row,productType);
    QString cropType = timeBox->currentText();
    if(!cropType.isEmpty() && !cropType.isNull()){
        zhongZhiBean->type = cropType;
    }else{
        emit logEvent("请选择买卖时辰");
        return false;
    }

    return true;
}


bool ZhongZhiTableWidget::saveBeans2File(){
    QList<ZhongZhiBean *> allRunshopBeans;
    if(getAllTableBeans(&allRunshopBeans)){
        QByteArray a= ZhongZhiBean::ArraytoJson(&allRunshopBeans);
        Setting::getInstance()->setZhongZhiJson(QString(a));
        return true;
    }
    qDeleteAll(allRunshopBeans.begin(), allRunshopBeans.end());
    return false;
}

void ZhongZhiTableWidget::readZhongZhiBeansSetting(QList<ZhongZhiBean> &zhongzhiBeans)
{
    QString json = Setting::getInstance()->getZhongZhiJson();
    zhongzhiBeans = ZhongZhiBean::jsonToArray(json.toUtf8());
}
